<?php
/*
	$table
	$page
	$rowsPerPage
	
	$columns
	$editURL
	$deleteURL
	$parentURL				//url den trang chua view, can cho phan chia trang
	$checkboxURL			//url khi kich vao checkbox
	$enableEdit
	$enableDelete
	$orders
	$groupby
	$imageColumns
	$thumbnailColumns
	$comboColumns
	$editableColumns
	$searchColumns
*/
if(!isset($parentURL))
	$parentURL = 'admin/quanly/xem/' . $table ;
if(!isset($page))
	$page = 0;
$CI = &get_instance();
$CI->load->library('Table_viewer');
$CI->load->database();
$query = $CI->db->query('show columns from ' . $table);
$checkboxColumns = array();
foreach($query->result() as $row)
{
	if(strpos($row->Type, 'tinyint') === 0)
	{
		if(isset($checkboxURL))
			$checkboxColumns[$row->Field] = $checkboxURL;
		else
			$checkboxColumns[$row->Field] = '';
	}
}

if(isset($rowsPerPage))
{
	$n = $CI->table_viewer->db->count_all($table);
	if($page * $rowsPerPage >= $n)
		$page = (int)(max($n-1,0) / $rowsPerPage);
}

$CI->table_viewer->db->from($table);
$CI->table_viewer->m_table = $table;
if(isset($rowsPerPage))
	$CI->table_viewer->db->limit($rowsPerPage, $page * $rowsPerPage);
if(isset($columns))
	$CI->table_viewer->m_columns = $columns;
if(isset($editURL))
	$CI->table_viewer->m_editURL = $editURL;
if(isset($deleteURL))
	$CI->table_viewer->m_deleteURL = $deleteURL;
if(isset($enableEdit))
	$CI->table_viewer->m_enableEdit = $enableEdit;
if(isset($enableDelete))
	$CI->table_viewer->m_enableDelete = $enableDelete;
$CI->table_viewer->m_param = $table . '/' . $page;
$CI->table_viewer->m_checkboxColumns = $checkboxColumns;
if(isset($searchColumns))
	$CI->table_viewer->m_searchColumns = $searchColumns;
if(isset($imageColumns))
{
	$CI->table_viewer->m_imageColumns = array();
	foreach($imageColumns as $r)
	{
		$CI->table_viewer->m_imageColumns[$r] = $r;
	}
}
if(isset($thumbnailColumns))
	$CI->table_viewer->m_thumbnailColumns = $thumbnailColumns;
if(isset($comboColumns))
	$CI->table_viewer->m_comboColumns = $comboColumns;
if(isset($editableColumns))
{
	$temp = array();
	foreach($editableColumns as $r)
	{
		$temp[$r] = $r;
	}
	$CI->table_viewer->m_editableColumns = $temp;
}
if(isset($orders))
{
	foreach($orders as $key=>$value)
	{
		$CI->table_viewer->db->order_by($key, $value);
	}
}
if(isset($groupby))
{
	$CI->table_viewer->db->group_by($groupby);
}
if(isset($comboColumns))
{
	foreach($comboColumns as $key=>$value)
	{
		if(($CI->session->userdata('filter/' . $table . '/' . $key) != NULL) && ($CI->session->userdata('filter/' . $table . '/' . $key) != -1))
			$CI->table_viewer->db->where($key, $CI->session->userdata('filter/'.$table.'/'.$key));
	}
} 
$query = $CI->db->query('show columns from ' . $table);
foreach($query->result() as $r)
{
	if($r->Type == 'datetime')
	{
		if($CI->session->userdata('check/'.$table.'/'.$r->Field))
		{
			$from = date('Y-m-d', 
						 $CI->session->userdata('datefrom/'.$table.'/'.$r->Field));
			$to = date('Y-m-d 23:59:59', $CI->session->userdata('dateto/'.$table.'/'.$r->Field));
			$CI->table_viewer->db->where($r->Field. " >= '$from'");
			$CI->table_viewer->db->where($r->Field . " <= '$to'");
		}
	}
}
if(isset($searchColumns))
{
	foreach($searchColumns as $key=>$value)
	{
		if($CI->session->userdata('search/'.$table.'/'.$key))
		{
			if($value == 'like')
			{
				if(strpos($CI->session->userdata('search/'.$table.'/'.$key), '*') !== FALSE)
				{
					$temp = str_replace('*', '%', $CI->session->userdata('search/'.$table.'/'.$key));
					$CI->table_viewer->db->where($key . ' like \'' . $temp . "'");
				}
				else
					$CI->table_viewer->db->like($key, $CI->session->userdata('search/'.$table.'/'.$key));
			}
			else if($value == 'between')
			{
				$temp = split('-', $CI->session->userdata('search/'.$table.'/'.$key));
				if(count($temp) >= 2)
				{
					$CI->table_viewer->db->where($key . ' >=', $temp[0]);
					$CI->table_viewer->db->where($key . ' <=', $temp[1]);
				}
				else
				{
					$temp = $CI->session->userdata('search/'.$table.'/'.$key);
					$temp = trim($temp);
					if(substr($temp, 0, 2) == '<=')
					{
						$CI->table_viewer->db->where($key . ' <=', substr($temp, 2));
					}
					else if(substr($temp, 0, 2) == '>=')
					{
						$CI->table_viewer->db->where($key . ' >=', substr($temp, 2));
					}
					else if(substr($temp, 0, 1) == '<')
					{
						$CI->table_viewer->db->where($key . ' <', substr($temp, 1));
					}
					else if(substr($temp, 0, 1) == '>')
					{
						$CI->table_viewer->db->where($key . ' >', substr($temp, 1));
					}
					else
					{
						$CI->table_viewer->db->where($key, $temp);
					}
				}
			}
		}
	}
}
//$CI->table_viewer->m_columns = array('ID_danhmucsanpham'=> 'ID');
//$CI->load->view('admin/form', array('width'=>700, 'title'=>$table, 'subview'=>NULL, 'data'=>'<div align="center"><br>' . $CI->table_viewer->show(TRUE) . '</div>'));
?>
<div align="center">
	<br />
<?php
echo $CI->table_viewer->show(TRUE);
?>
<?php 
if(isset($rowsPerPage))
{
	$sotrang = (int)(max($n-1,0) / $rowsPerPage);
?>
</div>
<div align="center">
<table cellspacing="10">
<tr>
<?php
	if($page > 0)
	{
?>
<td>
<a href="<?=site_url($parentURL. "/0");?>">Đầu</a>
</td>
<?php
	}
?>
<?php
	if($page > 0)
	{
		$prev = $page - 1;
		echo '<td>';
		echo '<a href="' . site_url($parentURL . "/$prev") . '">Trước</a>';
		echo '</td>';
	}
	
	for($i=max(0, $page-5); $i<$page; $i++)
	{
		echo '<td>';
		echo '<a href="' . site_url($parentURL . "/$i") . '">' . ($i+1) . '</a>';
		echo '</td>';
	}
	echo '<td>';
	echo '<a>'.($page + 1).'</a>';
	echo '</td>';
	
	for ($i=$page+1; $i<min($page+5, $sotrang+1); $i++)
	{
		echo '<td>';
		echo '<a href="' . site_url($parentURL . "/$i") . '">' . ($i+1) . '</a>';
		echo '</td>';
	}
	
	if($page < $sotrang)
	{
		$next = $page + 1;
		echo '<td>';
		echo '<a href="' . site_url($parentURL . "/$next") . '">Sau</a>';
		echo '</td>';
	}
?>
<?php
if($page < $sotrang)
{
?>
<td>
<a href="<?=site_url($parentURL . "/$sotrang"); ?>">Cuối</a>
</td>
<?php
}
?>
</tr>
</table>
</div>
<?php
}
?>
<?php
if(isset($table))
	unset($table);
if(isset($page))
	unset($page);
if(isset($rowsPerPage))
	unset($rowsPerPage);
if(isset($columns))
	unset($columns);
if(isset($editURL))
	unset($editURL);
if(isset($deleteURL))
	unset($deleteURL);
?>